package com.privatbank.Dao;

import com.privatbank.tables.Good;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Slava on 26.07.2014.
 */
public class GoodDao {
    private final String login;
    private final String password;
    private final String url;

    public GoodDao(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public List<Good> getAll() throws SQLException {
        List<Good> goods = new ArrayList<Good>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT * FROM Goods");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Good good = new Good();
                good.setIdManufacturer(rs.getInt(1));
                good.setModel(rs.getString(2));
                good.setIdCategory(rs.getInt(3));
                good.setPrice(rs.getDouble(4));
                good.setDescription(rs.getString(5));
                good.setImage(rs.getString(6));
                good.setStorage(rs.getString(7));
                goods.add(good);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return goods;
    }

    public List<Good> getCriterion(int categoryId) throws SQLException {
        List<Good> goods = new ArrayList<Good>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            con = DriverManager.getConnection(url, login, password);
            //StringBuilder sb = new StringBuilder();
            //sb.append("SELECT model, ID_manufacturer, storage, price FROM Good WHERE ID_category="+idCategory);
            stmt = con.prepareStatement("SELECT model, ID_manufacturer, storage, price FROM Good WHERE ID_category="+categoryId+"");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Good good = new Good();
                good.setModel(rs.getString(1));
                good.setIdManufacturer(rs.getInt(3));
                good.setStorage(rs.getString(4));
                good.setPrice(rs.getDouble(5));
                goods.add(good);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return goods;
    }
}


